﻿using DataProcess.Interface;
using DataProcess.Interface.Models;
using System;
using System.Text.RegularExpressions;

namespace DataProcessing.ShimadzuTXT
{
    public class DataProcessing : IDataProcessing
    {
        public string Name { get { return "Shimadzu(岛津) 荧光仪"; } }

        public IDataOutput Processing(string content)
        {
            XRayOutputModel outputModel = new XRayOutputModel();
            var regContext = new Regex("[\r\n]+").Replace(content, "\r");
            string[] spdata = regContext.Split('\r');
            outputModel.SampleName = spdata[0].Split(':')[1].Trim();
            outputModel.AnalyzeApplicationName = spdata[3].Split(':')[1].Trim();
            var time = new Regex("[\\s]+").Replace(spdata[4], " ").Split(new[] { ':' }, 2)[1].Trim();
            if (DateTime.TryParseExact(time, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime date))
            {
                outputModel.MeasureTime = date;
            }
            for (int i = 12; i < spdata.Length-4; i++)
            {
                var Data = new Regex("[\\s]+").Replace(spdata[i], " ").Split(' ');
                var AnalysisName = Data[0].Trim();
                var Result = Data[1].Trim();
                if (!outputModel.Eelements.ContainsKey(AnalysisName))
                {
                    outputModel.Eelements.Add(AnalysisName, Result);
                }
            }
            return outputModel;
        }
    }
}
